package com.mebs.model.service.commercial;

import com.mebs.model.Address;
import com.mebs.model.CurrencyName;
import com.mebs.model.service.Service;

import javax.persistence.*;
import java.util.Date;
import java.util.Set;

import org.jt.jet.util.JetUtils;
import org.jt.jet.annotation.Field;
import org.jt.jet.annotation.ArgumentType;

/**
 * Created by Sergey Prasolov, sap@jensentechnologies.com Date: Mar 10, 2006, 4:24:50 PM
 */
@Entity()
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "COMMER")
public class Commercial extends Service {
    @ManyToOne(optional = false)
    @JoinColumn(name = "LocationId")
    private Address location;

    @ManyToOne(optional = false)
    @JoinColumn(name = "TypeId")
    private CommercialType type;

    @ManyToOne(optional = true)
    @JoinColumn(name = "StatusId")
    private CommercialStatus status;

    @Column(name = "FacilitySpecs", nullable = false, length = 1024)
    private String facilitySpecs;

    @Column(name = "PrimaryContact", nullable = true, length = 250)
    private String primaryContact;

    @Column(name = "PrimaryPhone", nullable = true, length = 250)
    private String primaryPhone;

    @Column(name = "SecondaryContact", nullable = true, length = 250)
    private String secondaryContact;

    @Column(name = "SecondaryPhone", nullable = true, length = 250)
    private String secondaryPhone;

    @Column(name = "Commencement", nullable = true)
    private Date commencement;

    @Column(name = "Expiration", nullable = true)
    private Date expiration;

    @Column(name = "RatePerDay", nullable = true)
    private Double ratePerDay;

    @Column(name = "RatePerMonth", nullable = true)
    private Double ratePerMonth;

    @Column(name = "RatePerYear", nullable = true)
    private Double ratePerYear;

    @ManyToOne(optional = true)
    @JoinColumn(name = "CurrencyNameId")
    @Field(type = ArgumentType.INSTANCE)
    private CurrencyName currencyName;

    @Column(name = "Terms", nullable = true, length = 1024)
    private String terms;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "CommercialId")
    @OrderBy("expiration,extension")
    private Set<Prolongation> prolongations;

    @Column(name = "ClientLeaseOnFileContact", nullable = true, length = 1024)
    private String clientLeaseOnFileContact;

    @Column(name = "ClientLeaseOnFileLocation", nullable = true, length = 1024)
    private String clientLeaseOnFileLocation;

    @Column(name = "MEBSLeaseOnFileContact", nullable = true, length = 1024)
    private String mebsLeaseOnFileContact;

    @Column(name = "MEBSLeaseOnFileLocation", nullable = true, length = 1024)
    private String mebsLeaseOnFileLocation;

    /**
     * ********************** Getters-setters ***********************
     */

    public Address getLocation()
    {
        return location;
    }

    public void setLocation(Address location)
    {
        this.location = location;
    }

    public CommercialType getType()
    {
        return type;
    }

    public void setType(CommercialType type)
    {
        this.type = type;
    }

    public String getFacilitySpecs()
    {
        return facilitySpecs;
    }

    public void setFacilitySpecs(String facilitySpecs)
    {
        this.facilitySpecs = facilitySpecs;
    }

    public String getPrimaryContact()
    {
        return primaryContact;
    }

    public void setPrimaryContact(String primaryContact)
    {
        this.primaryContact = primaryContact;
    }

    public String getPrimaryPhone()
    {
        return primaryPhone;
    }

    public void setPrimaryPhone(String primaryPhone)
    {
        this.primaryPhone = primaryPhone;
    }

    public String getSecondaryContact()
    {
        return secondaryContact;
    }

    public void setSecondaryContact(String secondaryContact)
    {
        this.secondaryContact = secondaryContact;
    }

    public String getSecondaryPhone()
    {
        return secondaryPhone;
    }

    public void setSecondaryPhone(String secondaryPhone)
    {
        this.secondaryPhone = secondaryPhone;
    }

    public Date getCommencement()
    {
        return commencement;
    }

    public void setCommencement(Date commencement)
    {
        this.commencement = commencement;
    }

    public Set<Prolongation> getProlongations()
    {
        return prolongations;
    }

    public void setProlongations(Set<Prolongation> prolongations)
    {
        this.prolongations = prolongations;
    }

    public CommercialStatus getStatus()
    {
        return status;
    }

    public void setStatus(CommercialStatus status)
    {
        this.status = status;
    }

    public Date getExpiration()
    {
        return expiration;
    }

    public void setExpiration(Date expiration)
    {
        this.expiration = expiration;
    }

    public Double getRatePerDay()
    {
        return ratePerDay;
    }

    public void setRatePerDay(Double ratePerDay)
    {
        this.ratePerDay = ratePerDay;
    }

    public Double getRatePerMonth()
    {
        return ratePerMonth;
    }

    public void setRatePerMonth(Double ratePerMonth)
    {
        this.ratePerMonth = ratePerMonth;
    }

    public Double getRatePerYear()
    {
        return ratePerYear;
    }

    public void setRatePerYear(Double ratePerYear)
    {
        this.ratePerYear = ratePerYear;
    }

    public CurrencyName getCurrencyName()
    {
        return currencyName;
    }

    public void setCurrencyName(CurrencyName currencyName)
    {
        this.currencyName = currencyName;
    }

    public String getTerms()
    {
        return terms;
    }

    public void setTerms(String terms)
    {
        this.terms = terms;
    }

    public String getClientLeaseOnFileContact()
    {
        return clientLeaseOnFileContact;
    }

    public void setClientLeaseOnFileContact(String clientLeaseOnFileContact)
    {
        this.clientLeaseOnFileContact = clientLeaseOnFileContact;
    }

    public String getClientLeaseOnFileLocation()
    {
        return clientLeaseOnFileLocation;
    }

    public void setClientLeaseOnFileLocation(String clientLeaseOnFileLocation)
    {
        this.clientLeaseOnFileLocation = clientLeaseOnFileLocation;
    }

    public String getMebsLeaseOnFileContact()
    {
        return mebsLeaseOnFileContact;
    }

    public void setMebsLeaseOnFileContact(String mebsLeaseOnFileContact)
    {
        this.mebsLeaseOnFileContact = mebsLeaseOnFileContact;
    }

    public String getMebsLeaseOnFileLocation()
    {
        return mebsLeaseOnFileLocation;
    }

    public void setMebsLeaseOnFileLocation(String mebsLeaseOnFileLocation)
    {
        this.mebsLeaseOnFileLocation = mebsLeaseOnFileLocation;
    }

    @Transient
    public Type getRequirementType()
    {
        return Type.COMMERCIAL;
    }

    @Override
    public String toString()
    {
        return (this.getSequence() != null) ? this.getSequence().toString() : "";
    }

    @Transient
    public boolean isDeletable()
    {
        return JetUtils.isEmpty(this.getProlongations());
    }


    @Transient
    public String getSequenceNumber()
    {
        return String.format("COM-%s-%05d-%s",
            getCompany().getTicker() == null ? "XXX" : getCompany().getTicker(),
            getSequence(),
            type.toString().toUpperCase()
        );
    }
}
